//
//  ResetPassword.m
//  GKBHouse
//
//  Created by  on 12-8-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ResetPassword.h"
#import "GKBNavigationBar.h"
#import "iToast.h"
#import "WebServices.h"
@implementation ResetPassword

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self initTopBar];
    [self initView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
//}
- (void)initTopBar
{    
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(dismissView) withTitle:NSLocalizedString(@"resetPassWord", nil)];
    [self.view addSubview:topBar];    
    [topBar release];
}
- (void)dismissView
{
    [self dismissModalViewControllerAnimated:NO];
    [GKBMainController postStartRefresh];
}
- (void)initView
{
    [oldPasswordLabel setText:NSLocalizedString(@"oldPassword:", nil)];
    [newPasswordLabel setText:NSLocalizedString(@"new password:", nil)];
    [confirmPasswordLabel setText:NSLocalizedString(@"confirm:", nil)];
    [okButton setTitle:NSLocalizedString(@"ok", nil) forState:UIControlStateNormal];
    [okButton addTarget:self action:@selector(okAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)okAction
{
    [self resetPassword];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)resetPassword
{
    NSString *old = oldPassword.text;
    NSString *new = newPassword.text;
    NSString *confirm = confirmPassword.text;
    if ([old isEqualToString:@""]||[new isEqualToString:@""]||[new isEqualToString:@""]) {
        NSString *blank = NSLocalizedString(@"input blank", nil);
        [iToast showTip:blank gravity:iToastGravityCenter];
        return;
    }
    if (![new isEqualToString:confirm]) {
        NSString *inconsistent = NSLocalizedString(@"new password inconsistent", nil);
        [iToast showTip:inconsistent gravity:iToastGravityCenter];
        return;
    }
    
     
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"oldPasswd\":\"%@\", \"newPasswd1\":\"%@\", \"newPasswd2\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, old, new, confirm, sessionId];
    [WebServices setShowConnectting:NO];  
    
    [WebServices requestStartOrder:@"changePassword" body:body target:self action:@selector(resetPasswordEnd:) errorTip:nil];
}
- (void)resetPasswordEnd:(NSDictionary *)dic
{
    NSString *tip = @"";
    NSNumber *result = [dic objectForKey:@"result"];
    
    if (result.integerValue == 0) {   
        tip = NSLocalizedString(@"success", nil);
        //修改成功，退出到登录界面
        [LoginController1 setAutoLogin:NO];
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0) {
            [self dismissViewControllerAnimated:NO completion:^{
                [[GKBMainController mainCtr] dismissView];
            }];
            
        }
        else
        {
            [self dismissModalViewControllerAnimated:NO];
            [[GKBMainController mainCtr] dismissView];
        }
        
    }
    else
    {
       tip = NSLocalizedString(@"failure", nil);
    }
    //[iToast showTip:tip gravity:iToastGravityBottom];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:tip delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil];
    [alert show];
    [alert release];
}
@end
